package com.zhilehuo.pretask.ui.mine

import android.app.Application
import android.content.Context
import android.net.Uri
import android.os.Environment
import androidx.core.content.FileProvider
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

enum class Gender { MALE, FEMALE }

/**
 * @Description: 我的界面数据ViewModel
 * @Author: Realnt
 * @Date:
 */
class MineViewModel(application: Application) : AndroidViewModel(application) {
    private val _selectedGender = MutableLiveData(Gender.MALE)
    val selectedGender: LiveData<Gender> = _selectedGender

    private val _birthday = MutableLiveData<String>()
    val birthday: LiveData<String> = _birthday

    var tempImageUri: Uri? = null

    fun updateGender(gender: Gender) {
        _selectedGender.value = gender
    }

    fun setBirthday(date: String) {
        _birthday.value = date
    }

    /**
     * @Description: 创建一个临时的文件存储图片
     * @Param context : 为文件创建提供上下文
     * @Return Uri : 返回临时创建文件的Uri地址
     */
    fun createTempImageUri(context: Context): Uri? {
        return try {
            val tempFile = File.createTempFile(
                "JPEG_${SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())}_",
                ".jpg",
                context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
            )
            FileProvider.getUriForFile(
                context,
                "${context.packageName}.provider",
                tempFile
            ).also { uri ->
                tempImageUri = uri
            }
        } catch (e: IOException) {
            null
        }
    }
}